# _*_ coding: utf-8 _*_
from odoo import models, fields, api, tools


class StockQuantForPick(models.Model):
    _name = "stock.quant.for.pick"
    _auto = False

    product_id = fields.Many2one("product.product", string=u"产品", readonly=True)
    product_qty = fields.Float("数量", readonly=True)
    lot_id = fields.Many2one("stock.production.lot", string=u"批次", readonly=True)
    location_id = fields.Many2one("stock.location", string=u"库位", readonly=True)

    def init(self):
        tools.drop_view_if_exists(self._cr, 'stock_quant_for_pick')
        self._cr.execute("""
                CREATE or REPLACE VIEW stock_quant_for_pick AS (
                    select 
                    row_number() over() as id,
                    q.product_id, 
                    sum(q.qty) as product_qty, 
                    q.lot_id, 
                    q.location_id
                from stock_quant q
                       join stock_location s on s.id = q.location_id
                where s.usage = 'internal' and q.reservation_id is null
                group by q.product_id, q.lot_id, q.location_id
                )""")
